//
//  Tips.h
//  <20.0> 拖放操作
//
//  Created by chen on 16/9/21.




### 拖放 

开始拖放->数据复制进dragging粘贴板;
方下对象->数据从粘贴板读出。


typedef NS_OPTIONS(NSUInteger, NSDragOperation) {
    //常用的操作：
    NSDragOperationNone		= 0,
    NSDragOperationCopy		= 1,
    NSDragOperationLink		= 2,
    //比较少用的操作
    NSDragOperationGeneric	= 4,
    NSDragOperationPrivate	= 8,
    NSDragOperationMove		= 16,
    NSDragOperationDelete	= 32,
    NSDragOperationEvery	= NSUIntegerMax,
    
    NSDragOperationAll_Obsolete	NS_ENUM_DEPRECATED_MAC(10_0, 10_10) = 15, // Use NSDragOperationEvery
    NSDragOperationAll NS_ENUM_DEPRECATED_MAC(10_0, 10_10) = NSDragOperationAll_Obsolete, // Use NSDragOperationEvery
};

举例:
当拖放上一个图像的时候,需要协商好发生的操作。
1. 设置对象为动作的源；
2. 设置对象为拖动作的目的('NSDragOperation')。


####代码示例<1> 作为拖拽操作的源



//这个方法自动调用2次：:OutsideApplication/inApplication 程序间动作或者程序内部动作
- (NSDragOperation)draggingSession:(NSDraggingSession *)session sourceOperationMaskForDraggingContext:(NSDraggingContext)context{
    //NSDraggingContextOutsideApplication = 0,
    //NSDraggingContextWithinApplication
    return NSDragOperationCopy | NSDragOperationDelete; //(为了满足删除,添加 |Delete ,拖到垃圾站就会删除)
}
- (void)mouseDown:(NSEvent *)event{
    _mouseDownEvent = event ;
}

- (void)mouseDragged:(NSEvent *)event{
    
    NSPoint down = [_mouseDownEvent locationInWindow];
    NSPoint drag = [event locationInWindow];
    
    float distance = hypot(down.x - drag.x, down.y-drag.y);
    if (distance < 3) {
        return ;
    }
    
    if ([_myString length] == 0 ) {
        return ;
    }
    
    NSSize s = [_myString sizeWithAttributes:_attributes];
    //需要拖拽操作的对象
    NSImage *anImage = [[NSImage alloc]initWithSize:s];
    //在要绘制的图片创建矩形区域
    NSRect imagBounds ;
    imagBounds.origin = NSZeroPoint ;
    imagBounds.size = s ;
    //图片上绘制字符串
    [anImage lockFocus];
    [self drawStringCenteredIn:imagBounds];
    [anImage unlockFocus];
    
    //获取点击事件的位置
    NSPoint p = [self convertPoint:down fromView:nil]; //nil 表示相对于window
    
    //从图片中心开始拖
    p.x = p.x - s.width/2;
    p.y = p.y - s.height/2;
    
    //获取粘贴板
    NSPasteboard *pd = [NSPasteboard pasteboardWithName:NSDragPboard];
    //字符串放到粘贴板
    [self writeTPPasteboard:pd];
    
    //开始操作
    [self dragImage:anImage at:p offset:NSZeroSize event:_mouseDownEvent pasteboard:pd source:self slideBack:YES];
}

- (void)draggedImage:(NSImage *)image
endedAt:(NSPoint)screenPoint
operation:(NSDragOperation)operation{
    
    if (operation == NSDragOperationDelete) { //拖到垃圾站就会删除
        [self setMyString:@""];
    }
}


####代码示例<1> 作为拖拽操作的目的

1.注册支持复制string
[self registerForDraggedTypes:@[NSPasteboardTypeString]];


2.实现拖拽（入）

- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender{
    NSLog(@"draggingEntered:");
    if ([sender draggingSource] == self) {
        return NSDragOperationNone ;
    }
    _highlighted = YES  ;
    [self setNeedsDisplay:YES];
    return NSDragOperationCopy ;
}

- (void)draggingExited:(id<NSDraggingInfo>)sender{
    NSLog(@"draggingExited:");
    _highlighted = NO ;
    [self setNeedsDisplay:YES];
}

- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender{
    
    return YES ;
}

//处理拖拽事件
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender{
    
    NSPasteboard *pd = [sender draggingPasteboard];
    if ([self readFromPasteboard:pd] == NO) { //从粘贴板复制进来
        NSLog(@"Error:Could not read from draggingPasteboard");
        return NO ;
    }
    return YES ;
}

- (void)concludeDragOperation:(id<NSDraggingInfo>)sender{
    
    NSLog(@"concludeDragOperation:");
    _highlighted = NO ;
    [self setNeedsDisplay:YES];
}















